package guiPackage;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import adapterPackage.GemiAdapter;
import adapterPackage.GuzergahAdapter;

import systemPackage.LocalStrings;
import systemPackage.MainSystem;

import dataTypesPackage.Gemi;
import dataTypesPackage.Guzergah;
import dataTypesPackage.Musteri;
import dataTypesPackage.Sefer;



public class GUI_NewSefer extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4433274225172683743L;
	private JPanel createVoyagePanel = new JPanel();
	private JPanel enterPricesPanel = new JPanel();
	private List<Gemi> gemiListesi;
	private List<Guzergah> guzergahListesi;
	private Sefer sefer;
	private int currentMusteriType=0;
	private List<Integer> pricesForMusteriTypes = new ArrayList<Integer>();
	
	JLabel seferNameLabel = new JLabel(LocalStrings.getLocalString(27));
	JTextField seferNameField = new JTextField();
	JLabel gemiSelectLabel = new JLabel(LocalStrings.getLocalString(20));
	JComboBox gemiSelectionComboBox = new JComboBox();

	JLabel guzergahSelectLabel = new JLabel(LocalStrings.getLocalString(21));
	JComboBox guzergahSelectList = new JComboBox();

	JLabel musteriTypeLabel;
	JTextField musteriTypePriceField;
	
	JButton continueButton = new JButton(LocalStrings.getLocalString(22));
	public GUI_NewSefer(){
		gemiListesi = GemiAdapter.getAllGemi();
		guzergahListesi = GuzergahAdapter.getAllGuzergah();
		this.setSize(240, 220);
		createGui();
	}

	private void createGui() {
		// This mehod creates all the gui elements.
		for(int i=0;i<gemiListesi.size();i++){
			gemiSelectionComboBox.addItem(gemiListesi.get(i));
		}
		for(int i=0;i<guzergahListesi.size();i++){
			guzergahSelectList.addItem(guzergahListesi.get(i));
		}
		
		ActionListener continueButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method creates a new sefer object.
				// TODO make systemAdminID real.
				sefer = new Sefer(1, seferNameField.getText(), (Gemi)gemiSelectionComboBox.getSelectedItem(), (Guzergah)guzergahSelectList.getSelectedItem());
				setPricesWindow();
			}
		};
		continueButton.addActionListener(continueButtonPress);
		
		
		createVoyagePanel.setLayout(new GridLayout(7,2));
		createVoyagePanel.add(seferNameLabel);
		createVoyagePanel.add(seferNameField);
		createVoyagePanel.add(gemiSelectLabel);
		createVoyagePanel.add(gemiSelectionComboBox);
		createVoyagePanel.add(guzergahSelectLabel);
		createVoyagePanel.add(guzergahSelectList);
		createVoyagePanel.add(continueButton);
		
		this.add(createVoyagePanel);
		this.setVisible(true);

	}

	private void cleanSetPricesWindow() {
		// This method is used for freeing the used elements of setPrices
		enterPricesPanel.remove(musteriTypeLabel);
		enterPricesPanel.remove(musteriTypePriceField);
		enterPricesPanel.remove(continueButton);
		this.remove(enterPricesPanel);
		musteriTypeLabel = null;
		musteriTypePriceField = null;
		continueButton = null;
		this.setVisible(false);
		this.setVisible(true);
		setPricesWindow();
	}
	
	private void setPricesWindow() {
		// this window loops for Musteri.musteriTypes enum and sets prices.
		this.remove(createVoyagePanel);
		
		musteriTypeLabel = new JLabel(LocalStrings.getLocalString(24) + Musteri.MusteriTypes.values()[currentMusteriType]);
		if(Musteri.MusteriTypes.values().length == currentMusteriType+1)
			continueButton = new JButton(LocalStrings.getLocalString(25));
		else
			continueButton = new JButton(LocalStrings.getLocalString(14));								//14 says next.
		
		musteriTypePriceField = new JTextField();
		musteriTypePriceField.setColumns(15);
		enterPricesPanel.add(musteriTypeLabel);
		enterPricesPanel.add(musteriTypePriceField);
		
		ActionListener nextMusteriTypeButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method decides if there is another musteri type, and if there is start loop again.
				pricesForMusteriTypes.add(Integer.parseInt(musteriTypePriceField.getText()));
				if(Musteri.MusteriTypes.values().length == currentMusteriType+1){
					sefer.setFiyatlar(pricesForMusteriTypes);
					MainSystem.set(sefer, Sefer.class);
					dispose();
				}
				else{
					currentMusteriType++;
					cleanSetPricesWindow();
				}
				
			}
		};
		continueButton.addActionListener(nextMusteriTypeButtonPress);
		enterPricesPanel.add(continueButton);
		this.add(enterPricesPanel);
		this.setVisible(false);
		this.setVisible(true);
	}
}
